//
//  DWRunLoopViewController.m
//  OCAPPDemo
//
//  Created by 杨冬武 on 2021/4/18.
//

#import "DWRunLoopViewController.h"
#import "DWWeakTimerTool.h"
#import "DWTimerProxy.h"

@interface DWRunLoopViewController ()

@property (nonatomic, strong) NSTimer *timer;

@end

@implementation DWRunLoopViewController

- (void)dealloc {
    [self.timer invalidate];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    NSRunLoop *rlCurrent = [NSRunLoop currentRunLoop];
    
    //主线程的RunLoop
    //NSRunLoop *rlMain = [NSRunLoop mainRunLoop];
    
    //运行RunLoop
    //[rlCurrent run];
    //[rlCurrent runUntilDate:[NSDate distantFuture]];
    //[rlCurrent runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    
    NSLog(@"---------------------");
    
    //获取指定NSRunLoop中的CFRunLoopRef对象
    CFRunLoopRef cfRunLoop = [rlCurrent getCFRunLoop];
    
    //获取CFRunLoopRef
    CFRunLoopRef rlrCurrent = CFRunLoopGetCurrent();
    CFRunLoopRef rlrMain = CFRunLoopGetMain();
    //NSLog(@"%@", rlrCurrent);
    
    //执行当前线程的CFRunLoopRef
    //CFRunLoopRun();
    //CFRunLoopRunResult result = CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, YES);
    
    //退出RunLoop,通过CFRun方式运行的可以停止.
    //CFRunLoopStop(rlrCurrent);
    
    
    //添加端口
    /*
    NSPort *port = [NSPort port];
    [rlCurrent addPort:port forMode:NSDefaultRunLoopMode];
    NSLog(@"%@", rlCurrent);
    */
    
    
//    self.timer = [NSTimer scheduledTimerWithTimeInterval:3 repeats:YES block:^(NSTimer * _Nonnull timer) {
//        
//    }];
    
    
    //[self addAnObserverToCurrentRunLoop];
    
}

- (void)addAnObserverToCurrentRunLoop {
    CFRunLoopObserverRef observer = CFRunLoopObserverCreateWithHandler(kCFAllocatorDefault, kCFRunLoopAllActivities, YES, INT8_MAX, ^(CFRunLoopObserverRef observer, CFRunLoopActivity activity) {
        switch (activity) {
            case kCFRunLoopEntry:
                NSLog(@"kCFRunLoopEntry");
                break;
            case kCFRunLoopBeforeTimers:
                NSLog(@"kCFRunLoopBeforeTimers");
                break;
            case kCFRunLoopBeforeSources:
                NSLog(@"kCFRunLoopBeforeSources");
                break;
            case kCFRunLoopBeforeWaiting:
                NSLog(@"kCFRunLoopBeforeWaiting");
                break;
            case kCFRunLoopAfterWaiting:
                NSLog(@"kCFRunLoopAfterWaiting");
                break;
            case kCFRunLoopExit:
                NSLog(@"kCFRunLoopExit");
                break;
            default:
                break;
        }
    });
    CFRunLoopAddObserver(CFRunLoopGetCurrent(), observer, kCFRunLoopDefaultMode);
}



@end
